package com.foreknow.protals.common.util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class TestStream {
    /**
     * 筛选集合
     * 筛选address为沈阳的学生
     * @param students
     * @return
     */
    public static List<Student> testFilter(List<Student> students) {
        return students.stream().filter(s -> "沈阳".equals(s.getAddress())).collect(Collectors.toList());
    }

    /**
     * 转换元素的值
     * 在地址前面加上部分信息,只输出地址的信息
     * @param students
     */
    public static void testMap(List<Student> students) {
        List<String> newAddress = students.stream().map(s -> "住址" + s.getAddress()).collect(Collectors.toList());
        newAddress.forEach(a -> System.out.println(a));
    }

    /**
     * 去重
     * 对简单的字符串去重复
     */
    public static void testDistinct1() {
        List<String> list = Arrays.asList("111","222","333","111","222");
        list.stream().distinct().forEach(System.out::println);
    }

    /**
     * 对集合中的元素去重复数据
     * 需要注意的是Student对象中一定要重写equals和hashCode方法否则去重复无效
     */
    public static void testDistinct2() {
        Student s1 = new Student(1L,"张三",15,"沈阳");
        Student s2 = new Student(2L,"王五",15,"江西");
        Student s3 = new Student(3L,"李四",17,"北京");
        Student s4 = new Student(4L,"杨明",17,"沈阳");
        Student s5 = new Student(1L,"张三",15,"沈阳");
        List<Student> students = new ArrayList<>();
        students.add(s1);
        students.add(s2);
        students.add(s3);
        students.add(s4);
        students.add(s5);
        students.stream().distinct().forEach(System.out::println);
    }

    /**
     * 对集合中的元素排序
     */
    public static void testSorted1() {
        List<String> list = Arrays.asList("333","222","111");
        list.stream().sorted().forEach(System.out::println);
    }

    /**
     * 集合排序，可以指定排序规则
     * 先按照学生的id降序排序，再按照年龄降序排序
     */
    public static void testSorted2() {
        Student s1 = new Student(1L,"张三",15,"沈阳");
        Student s2 = new Student(2L,"王五",15,"江西");
        Student s3 = new Student(3L,"李四",17,"北京");
        Student s4 = new Student(4L,"杨明",17,"沈阳");

        List<Student> list = new ArrayList<>();
        list.add(s1);
        list.add(s2);
        list.add(s3);
        list.add(s4);

        list.stream()
                .sorted((stu1,stu2) -> Long.compare(stu2.getId(),stu1.getId()))
                .sorted((stu1,stu2) -> Integer.compare(stu2.getAge(),stu1.getAge())).
                forEach(System.out::println);
    }



    public static void main(String[] args) {
        Student s1 = new Student(1L,"张三",15,"沈阳");
        Student s2 = new Student(2L,"王五",15,"江西");
        Student s3 = new Student(3L,"李四",17,"北京");
        Student s4 = new Student(4L,"杨明",17,"沈阳");
        Student s5 = new Student(1L,"张三",15,"沈阳");
        List<Student> list = new ArrayList<>();
        list.add(s1);
        list.add(s2);
        list.add(s3);
        list.add(s4);

        testSorted2();

//        testSorted1();

//        testDistinct2();

//        List<Student> streamList = testFilter(list);
//        for (Student student:streamList) {
//            System.out.println(student.getName()+"---"+student.getAge());
//        }
//        testMap(list);
    }
}
